package com.mobilepay.moneypig.model.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.config.Key;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.model.dialog.PasswordDialog;
import com.mobilepay.moneypig.model.dialog.TipDialog;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.PreferenceUtil;

/**
 * Created by xlm on 2017/7/24 0024
 */

public class TixianActivity extends BaseActivity {
    private static final String TAG = "TixianActivity";
    private RelativeLayout mRelativeBack;
    private RelativeLayout mRelativeAll;
    private TextView mTextBank;
    private TextView mTextpay;
    private TextView mTextBlance;
    private TextView mTixianFei;
    private EditText mEditMoney;
    private Button mButtonTixian;
    String balance;
    String tixianfei;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_tixian);
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.tixian_back);
        mTextBank = (TextView) findViewById(R.id.card_bank);
        mTextpay = (TextView) findViewById(R.id.card_pay);
        mTextBlance = (TextView) findViewById(R.id.tixian_blance);
        mRelativeAll = (RelativeLayout) findViewById(R.id.tixian_all);
        mEditMoney = (EditText) findViewById(R.id.tixian_money);
        mButtonTixian = (Button) findViewById(R.id.tixian_btn);
        mTixianFei = (TextView) findViewById(R.id.tixian_fei);
    }

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        balance = intent.getStringExtra("money");
        tixianfei = intent.getStringExtra("tixianfei");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        String bankName = App.getApp().getUserDetailInfo().getData().getBank_name();
        String cardNum = App.getApp().getUserDetailInfo().getData().getBank_card_code();
        if (!CommonUtil.isEmpty(bankName)) {
            mTextBank.setText(bankName);
        }
        if (!CommonUtil.isEmpty(cardNum)) {
            String newString = "";
            newString = CommonUtil.StringOpt(cardNum, 3, cardNum.length() - 4);
//            if (cardNum.length() > 16) {
//                newString = CommonUtil.StringOpt(cardNum, 3, 16);
//            } else {
//                newString = CommonUtil.StringOpt(cardNum, 3, 12);
//            }
            mTextpay.setText(newString);
        }
        if (!CommonUtil.isEmpty(balance)) {
            mTextBlance.setText("当前余额" + balance + "元");
        } else {
            mTextBlance.setText("当前余额" + "0.00" + "元");
        }
        if (!CommonUtil.isEmpty(tixianfei)) {
            mTixianFei.setText(tixianfei);
        } else {
            mTixianFei.setText("");
        }
    }

    private TipDialog confirmVipDialog;

    private void showVipConfirm(String txt) {
        if (confirmVipDialog == null) {
            confirmVipDialog = new TipDialog(this);
        }
        confirmVipDialog.setContent(txt);
        confirmVipDialog.setCancelable(true);
        confirmVipDialog.setConfirm("去升级");
        confirmVipDialog.show();
        confirmVipDialog.setListener(new TipDialog.Listener() {
            @Override
            public void onConfirm() {
                super.onConfirm();
                confirmVipDialog.dismiss();
                startActivity(VipActivity.class, false, false);
            }

            @Override
            public void onDismiss() {
                super.onDismiss();
                confirmVipDialog.dismiss();
            }
        });
    }

    private static int DECIMAL_DIGITS = 2;

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(this);
        mRelativeAll.setOnClickListener(this);
        mButtonTixian.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onClick(View v) {
                super.onClick(v);
                String money = mEditMoney.getText().toString();
                if (CommonUtil.isEmpty(money)) {
                    showToastShort("请输入提现金额");
                } else {
                    int grade = App.getApp().getUserDetailInfo().getData().getGrade();
                    if (grade == 1) {
                        showVipConfirm("请升级经纪商会员");
                    } else {
                        showPasswordDialog();
                    }
                }
            }

            @Override
            public void onNoDoubleClick(View view) {
                super.onNoDoubleClick(view);
            }
        });

        //限制输入框 5位整数，，2位小数
        mEditMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > DECIMAL_DIGITS) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + DECIMAL_DIGITS + 1);
                        mEditMoney.setText(s);
                        mEditMoney.setSelection(s.length());
                    }
                }
                if (s.toString().trim().equals(".")) {
                    s = "0" + s;
                    mEditMoney.setText(s);
                    mEditMoney.setSelection(2);
                }
                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        mEditMoney.setText(s.subSequence(0, 1));
                        mEditMoney.setSelection(1);
                    }
                }
                if (s.toString().contains(".")) {
                    String num = s.toString().substring(0, s.toString().indexOf("."));
                    if (num.length() > 5) {
                        mEditMoney.setText(s.subSequence(0, 5));
                    }
                } else {
                    String num = s.toString();
                    if (num.length() > 5) {
                        mEditMoney.setText(s.subSequence(0, 5));
                        mEditMoney.setSelection(s.subSequence(0, 5).length());
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tixian_back:
                finish();
                break;
            case R.id.tixian_all:
                if (!"0.00".equals(balance)) {
                    mEditMoney.setText(balance);
                    mEditMoney.setSelection(balance.length());
                } else {
                    showToastShort("当前余额为零");
                }
                break;
        }
    }

    private PasswordDialog passwordDialog;

    private void showPasswordDialog() {
        if (passwordDialog == null) {
            passwordDialog = new PasswordDialog(this);
        }
        passwordDialog.setCancelable(true);
        passwordDialog.show();
        passwordDialog.setListener(new PasswordDialog.Listener() {
            @Override
            public void onConfirm(String ps) {
                if (CommonUtil.isEmpty(ps)) {
                    showToastShort("请输入登陆密码");
                } else {
                    String password = (String) PreferenceUtil.getParam(getThis(), Key.PASSWORD, "");
                    if (!CommonUtil.isEmpty(password) && !password.equals(ps)) {
                        showToastShort("密码错误");
                    } else {
                        passwordDialog.dismiss();
                        tixianCommit();
                    }
                }
            }
        });
    }

    private void tixianCommit() {
        String money = mEditMoney.getText().toString();
        if (CommonUtil.isEmpty(money)) {
            showToastShort("提现金额不能为空");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.tixian);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("money", money);
        param.add("sign", param.getMd5String());
        showProgress("正在处理");
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissProgress();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissProgress();
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    showToastShort(responseResult.getInfo());
                    if (responseResult.getStatus() == 1) {
                        Intent intent = new Intent();
                        intent.setClass(TixianActivity.this, InComeActivity.class);
                        startActivity(intent, true);
                    }
                }
            }
        });
    }

}

